package br.com.ava.core.funciona;

import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import br.com.ava.core.infra.persistencia.AbstractMediator;

@SuppressWarnings("serial")
public class FuncionaMediator extends AbstractMediator<Funciona> {

	private FuncionaDAO dao;

	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public List<Funciona> consultarTodas() {
		return dao.listar();
	}

	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public Funciona buscar(Integer id) {
		return dao.buscar(id);
	}

	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public void salvar(Funciona funciona) {
		validarInclusao(funciona);
		funciona.setIdFuncionario(obterProximoId());
		funciona.setDataAdmissao(new Date());
		dao.salvar(funciona);
	}

	private Integer obterProximoId() {
		Integer retorno = dao.obterIdAtual();
		if (retorno == null) {
			retorno = 1;
		} else {
			retorno++;
		}
		return retorno;
	}

	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public Funciona buscarPorEmail(String email) {
		return dao.buscarPorEmail(email);
	}

	@Override
	protected FuncionaDAO getDao() {
		return dao;
	}

	public void setDao(FuncionaDAO dao) {
		this.dao = dao;
	}

}
